using System;
using Godot;

public partial class GameManager : Node
{
    //[Signal] public delegate void OnGameStartEventHandler();

    [Export] private Node sceneNode;
    [Export] private Node uiNode;

    [Export] private Node currentScene;
    [Export] private TitleMenuController titleMenu;
    [Export] private PauseMenuController pauseMenu;

    [Export] private bool gameIsPaused = false;

    public override void _Ready()
    {
        GD.Print("GameManager ready!");

        GameInit();
    }

    public override void _Process(double delta)
    {
        if (Input.IsActionJustPressed("Pause"))
        {
            PauseGame();
        }
    }

    private void GameInit()
    {
        titleMenu = GetNode<TitleMenuController>("/root/Game/UI/TitleMenu");
        pauseMenu = GetNode<PauseMenuController>("/root/Game/UI/PauseMenu");

        pauseMenu.UnVisible();
    }

    private void GameStart()
    {
        // 游戏开始逻辑
        PackedScene scene = ResourceLoader.Load<PackedScene>("res://Scenes/TestScene.tscn");
        Node gameScene = scene.Instantiate();
        sceneNode.AddChild(gameScene);
        currentScene = gameScene;

        titleMenu.UnVisible();
    }

    private void PauseGame()
    {
        // 切换游戏暂停状态
        GetTree().Paused = !gameIsPaused;
        gameIsPaused = !gameIsPaused;

        if(gameIsPaused)
        {
            pauseMenu.OnVisible();
        }
        else
        {
            pauseMenu.UnVisible();
        }
    }

    private void GameOver()
    {
        PauseGame();
        titleMenu.OnVisible();
        
        currentScene.QueueFree();
        currentScene = null;
    }

    private void QuitGame()
    {
        //离开游戏
        GetTree().Quit();
    }
}
